<?php

	class source
	{

		var $fw		= null;
		var $sec	= null;

		function source(&$framework, &$security)
		{

			$this->fw	= &$framework;
			$this->sec	= &$security;

			switch($this->fw->do)
			{

				case 'advanced':
					$this->show_search_form();
					break;

				default:
					$this->process_search();
					break;

			}

		}

		function process_search()
		{

			$search	= trim(@$_REQUEST['search']);

			if(!empty($search))
			{

				// Paper Chain

				$this->fw->template = 'search.results.tpl';

				$this->fw->pc_item('Search', 'Search for movies with your database', 'index.php?what=search&do=advanced');
				$this->fw->pc_item('Results For '. ucwords(htmlentities($search)), 'Results found for '. ucwords(htmlentities($search)), 'index.php?what=search&search='. $search);


				$filter	= @$_REQUEST['filter'];

				// pagination
				$results	= $this->fw->get_search($search, $filter);
				$num		= count($results);
				$pagin		= $this->fw->pagination($num);

				if(is_array($results)) {
					$results = array_slice($results, $pagin['start'], $pagin['limit']);
				}

				$this->fw->content['search_results']		= $results;
				$this->fw->content['pagination']			= $pagin;

				$this->fw->content['alpha_base']	= 'index.php?what=search&search='. $search;

				$this->fw->content['filter']		= $filter;
				$this->fw->content['search']	= htmlentities($search);

			}
			else
			{

				$this->show_search_form();

			}

		}

		function show_search_form()
		{

			// Paper Chain
			$this->fw->pc_item('Search', 'Search for movies with your database', 'index.php?what=search&do=advanced');
			$this->fw->pc_item('Advanced', 'Search for movies with your database', 'index.php?what=search&do=advanced');

			$this->fw->js[]		= 'search.js';

			$this->fw->template = 'search.form.tpl';

			$genres = $this->fw->get_genres();

			// Form Data
			$this->fw->content['types']			= array('database' 		=> ' Movie Database',
														'collection'	=> ' My Collection',
														'wishlist'		=> ' My Wishlist'/*,
														'borrow'		=> ' Borrowed'*/);


			$this->fw->content['genres']		= $this->fw->_convert_array($genres, 'id', 'name');

			// Form values
			$this->fw->content['type']			= 'database';
			$this->fw->content['genre']			= $this->fw->_convert_array($genres, 'id', 'id');

		}

	}

?>